package com.desire.test;

import java.util.*;

public class _12_list集合 {

    public static void main(String[] args) {
        //固定的List，相当于一个特殊的数组
        List<String> list = List.of("java", "javascript", "php", "python", "c",
                "go", "ruby");
        list = new ArrayList<String>(list);
        System.out.println(list);

    }

    public static void main4(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.addAll(List.of(1, 6, 3, 20, 9, 5));
        //数字
        System.out.println(list);
        Collections.sort(list);
        list.sort((a, b) -> b - a); //降序
        System.out.println(list);
        list.sort((a, b) -> a - b); //升序
        System.out.println(list);
        Collections.shuffle(list); //乱序
        System.out.println(list);


//字符串字符串List<String> 排序
        List<String> ls = new ArrayList<>
                (List.of("javascript", "c", "go", "ruby", "java", "php", "python"));
        System.out.println(ls);
        Collections.shuffle(ls);//乱序
        System.out.println(ls);
        Collections.sort(ls);//升序
        System.out.println(ls);
        Collections.sort(ls, Comparator.reverseOrder());//降序
        System.out.println(ls);
//字符串根据长度升序
        Collections.sort(ls, (a, b) -> a.length() - b.length());
        System.out.println(ls);
//字符串根据长度降序
        Collections.sort(ls, (x, y) -> y.length() - x.length());
        System.out.println(ls);

    }

    public static void main3(String[] args) {
        //数组转换List
        Integer[] nums = new Integer[]{100, 200, 300};
        var list6 = new ArrayList<Integer>(Arrays.asList(nums));
        list6.clear();
        System.out.println(list6);

        //List转换为Array数组
        List<String> list7 = new ArrayList<>(List.of("java", "springmvc", "springboot"));
        System.out.println(list7.size());
        list7.remove("springmvc");


    }

    public static void main2(String[] args) {
        //实例化ArrayList<String> 使用泛型
        var list = new ArrayList<String>();
        list.add("javascript");
        list.add(0, "java");
        list.addAll(1, List.of("php", "python", "ruby", "python", "mysql", "python"));
        //遍历集合方法一
        for (String s : list) {
            System.out.println(s);
        }
        //遍历集合方法二
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //遍历集合方法三
        list.forEach(System.out::println);

        System.out.println(list.contains("html")); //false
        //true
        System.out.println(list.containsAll(List.of("java", "mysql"))); //true
        System.out.println(list.indexOf("java"));//0
        System.out.println(list.lastIndexOf("java"));//0

        //判断是不是空的
        System.out.println(list.isEmpty());//false
        //删除从左边开始第一次出现的python
        //list.remove("python");
        //删除指定位置的内容
        //list.remove(3);
        //删除所有python
        //list.removeAll(List.of("python"));
        //删除所有元素，清空集合

        list.clear();
        //返回list元素个数
        System.out.println(list.size());
        System.out.println(list);

    }

    public static void main1(String[] args) {
        //实例化
        List<String> list = new ArrayList<>();

        list.add("你好");
        list.addAll(List.of("java", "php", "js"));

        list.indexOf("java");
        list.set(1, "j");

        System.out.println(list.contains("java"));

        list.remove("php");
        list.removeAll(List.of("你好", "java"));

        //如果有py 就删除
        list.removeIf(e -> e.contains("py"));
        // list.removeIf(item -> true);
        System.out.println(list);
    }
}
